var PageSize = 5;
var paged = 1;
var userType = GetUser("userType");
var goodspj = GetUser("goodspj");
//批量删除
$("#DIB2").click(function () {
  for (let i = 0; i < userType.length; i++) {
    if ($(".check").eq(i).prop("checked") == true) {
      userType.splice(i, 1);
      layer.msg("已删除", { icon: 1, time: 500 });
    }
  }
  fenye();
  Typeappend(userType);
});
//批量删除
$("#DIB5").click(function () {
  for (let i = 0; i < goodspj.length; i++) {
    // console.log($(".check").eq(i).prop("checked"));
    if ($(".check").eq(i).prop("checked") == true) {
      goodspj.splice(i, 1);
      layer.msg("已删除", { icon: 1, time: 500 });
    }
  }
  fenye1(goodspj);
  Goodspjappend(goodspj);
});
//分页
function fenye() {
  let Toltalpage = Math.ceil(userType.length / PageSize);
  // if ($("#type_page").html() == "") {
  //   Toltalpage--;
  // }
  //渲染前先清空
  $("#type_page").html("");
  $("#type_page").append(`<button id="DIB3">上一页</button>
    <span class="datepage"></span>
    <button id="DIB4">下一页</button>`);

  for (var i = 1; i <= Toltalpage; i++) {
    $(".datepage").append(`<button class="page_btn">${i}</button>`);
    $(".datepage").on("click", ".page_btn", function () {
      var index = this.innerHTML;
      // console.log($(this));
      $(this).addClass("cyan").siblings().removeClass("cyan");
      // var ins = document.getElementsByClassName("page_btn")[0].innerHTML;
      // console.log(ins);
      paged = index;
      Typeappend(userType);
      // console.log(page);
    });
  }
  $(".page_btn").eq(0).addClass("cyan").siblings().removeClass("cyan");
  //上一页
  $("#DIB3").click(function () {
    if (paged > 1) {
      paged--;
    }
    $(".page_btn")
      .eq(paged - 1)
      .addClass("cyan")
      .siblings()
      .removeClass("cyan");
    Typeappend(userType);
    checkAllChecked();
  });
  //下一页
  $("#DIB4").click(function () {
    // let Toltalpage = Math.ceil(userType.length / PageSize);
    if (paged < Toltalpage) {
      paged++;
    }
    $(".page_btn")
      .eq(paged - 1)
      .addClass("cyan")
      .siblings()
      .removeClass("cyan");
    Typeappend(userType);
    checkAllChecked();
  });
}
function fenye1(goodspj) {
  let Toltalpage = Math.ceil(goodspj.length / PageSize);
  // if ($("#type_page1").html() == "") {
  //   Toltalpage--;
  // }
  $("#type_page1").html("");
  $("#type_page1").append(`<button id="DIB3">上一页</button>
    <span class="datepage1"></span>
    <button id="DIB4">下一页</button>`);

  for (var i = 1; i <= Toltalpage; i++) {
    $(".datepage1").append(`<button class="page_btn1">${i}</button>`);

    $(".datepage1").on("click", ".page_btn1", function () {
      var index = this.innerHTML;
      $(this).addClass("cyan").siblings().removeClass("cyan");
      // var ins = document.getElementsByClassName("page_btn")[0].innerHTML;
      // console.log(ins);
      paged = index;
      Goodspjappend(goodspj);
      // console.log(page);
    });
  }
  $(".page_btn1").eq(0).addClass("cyan").siblings().removeClass("cyan");
  //上一页
  $("#DIB3").click(function () {
    if (paged > 1) {
      paged--;
    }
    $(".page_btn1")
      .eq(paged - 1)
      .addClass("cyan")
      .siblings()
      .removeClass("cyan");
    Goodspjappend(goodspj);
  });
  //下一页
  $("#DIB4").click(function () {
    // let Toltalpage = Math.ceil(userType.length / PageSize);
    if (paged < Toltalpage) {
      paged++;
    }
    $(".page_btn1")
      .eq(paged - 1)
      .addClass("cyan")
      .siblings()
      .removeClass("cyan");
    Goodspjappend(goodspj);
  });
}
fenye1(goodspj);
//分页
fenye();

//类型管理表格
Typeappend(userType);
function Typeappend(userType) {
  //分页模板
  let Toltalpage = Math.ceil(userType.length / PageSize);
  // if ($("#type_page").html() == "") {
  //   Toltalpage--;
  // }
  // console.log(Toltalpage);
  //每次渲染前先清空
  $("#tbody-1").html("");
  // console.log(Toltalpage);
  for (let i = 0; i < userType.length; i++) {
    if (paged <= Toltalpage && i < paged * PageSize && i >= (paged - 1) * PageSize) {
      $("#tbody-1").append(`<tr>
            <td><input class="check" type="checkbox"></td>
            <td>${i + 1}</td>
            <td>${userType[i].typeName}</td>
            <td>${userType[i].Prefix}</td>
            <td>${userType[i].suffix}</td>
            <td>${userType[i].Attributes}</td>
            <td>
              <button type="button" index-id="${
                userType[i].ID
              }" class="layui-btn layui-btn-sm laycc"><i class="layui-icon"></i></button>
              <button id="cc" index-id="${
                userType[i].ID
              }" type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                style="background-color: red;"><i class="layui-icon"></i></button>
            </td>
          </tr>`);
    }
  }
}
//渲染商品评价
Goodspjappend(goodspj);
function Goodspjappend(goodspj) {
  let Toltalpage = Math.ceil(goodspj.length / PageSize);
  // if ($("#type_page1").html() == "") {
  //   Toltalpage--;
  // }
  //每次渲染前先清空
  $("#tbody-2").html("");
  for (let i = 0; i < goodspj.length; i++) {
    if (paged <= Toltalpage && i < paged * PageSize && i >= (paged - 1) * PageSize) {
      $("#tbody-2").append(`<tr>
            <td><input class="check" type="checkbox"></td>
            <td>${i + 1}</td>
            <td>${goodspj[i].pjdate}</td>
            <td>${goodspj[i].pjgoods}</td>
            <td>${goodspj[i].pjuser}</td>
            <td>
              <button type="button" index-id="${
                goodspj[i].ID
              }" class="layui-btn layui-btn-sm layccpj"><i class="layui-icon">&#xe65f;</i></button>
              <button id="cc" index-id="${
                goodspj[i].ID
              }" type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                style="background-color: red;"><i class="layui-icon"></i></button>
            </td>
          </tr>`);
    }
  }
}
